<!--
 * @Desc:
 * @author cheegon
 * @date 2024/07/02  下午3:49
-->
<template>
  <div>
    <!--    面包屑导航区-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/essays' }">随笔管理</el-breadcrumb-item>
      <el-breadcrumb-item>随笔发布</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="hover">
      <el-form label-position="left" label-width="80px" style="text-align: left" ref="formRef"
               :model="formData">
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title"></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <SimpleUpload :info="imageInfo" ref="uploadRef" @complete="handleUploadComplete"></SimpleUpload>
        </el-form-item>
        <el-form-item label="色彩" prop="color">
          <el-color-picker size="small" show-alpha v-model="formData.color"></el-color-picker>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <mavon-editor v-model="formData.content"
                        ref="mdRef"
                        @imgAdd="imgAdd"
                        style="margin: 10px auto;min-height: 70vh"/>
        </el-form-item>
        <el-form-item style="text-align: center">
          <el-button type="primary" @click="handleSubmit">发布</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import SimpleUpload from '@/components/SimpleUpload.vue'
import {
  reqAddEssay,
  reqEditEssay,
  reqEssayDetail,
} from '@/api/adminApi'
import { reqUpload } from '@/api/uploadApi'

export default {
  name: "AEditEssay",
  components: { SimpleUpload },
  props: [],
  data() {
    return {
      id: 0,
      isEdit: false,
      formData: {
        image: '',
        title: '',
        content: '',
        color: 'rgba(0, 0, 0, 1)',
      },
      formRules: {
        title: [
          { required: true, message: '随笔标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '随笔内容不能为空', trigger: 'blur' }
        ],
      },
      imageInfo: {},
    }
  },
  created() {
    if (this.$route.path == '/admin/essay/edit') {
      this.isEdit = true
      this.id = this.$route.query.id
      this.formData.id = this.id
      this.getInfo()
    }
  },
  methods: {
    getInfo() {
      reqEssayDetail(this.id).then(data => {
        this.formData = data
        this.imageInfo.imageList = [data.image]
        this.$refs.uploadRef.refresh()
      })
    },
    handleUploadComplete(value) {
      if (value && value.length) {
        this.formData.image = value[0]
      } else {
        this.formData.image = ''
      }
    },
    // 将图片上传到服务器，返回的地址替换到md中
    async imgAdd(pos, file) {
      const form = new FormData()
      form.append('file', file)
      reqUpload(form).then(data => {
        this.$refs.mdRef.$img2Url(pos, data.url)
      })
    },
    handleSubmit() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return;
        this.formData.html=this.$refs.mdRef.d_render
        if (this.isEdit) {
          this.handleEdit()
        } else {
          this.handleAdd()
        }
      })
    },
    // 发布项目
    handleAdd() {
      reqAddEssay(this.formData).then(data => {
        this.$message.success('发布成功！')
        this.$router.back()
      })
    },
    // 更新项目
    handleEdit() {
      reqEditEssay(this.formData).then(data => {
        this.$message.success('更新成功！')
        this.$router.back()
      })
    },
  }
}
</script>

<style lang="stylus" scoped>

</style>
